<template>
  <div class="app-container">
    <el-row class="header">
      <el-col class="asd" :span="18">
        <el-select clearable v-model="data.params.initiator" placeholder="发启者">
          <el-option
            v-for="item in (data.allUserList as any)"
            :key="item.userId"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
        <el-select clearable v-model="data.params.level" placeholder="预警等级">
          <el-option
            v-for="item in (data.warningLevel as any)"
            :key="item.itemValue"
            :label="item.itemText"
            :value="item.itemText"
          />
        </el-select>
        <el-select clearable v-model="data.params.type" placeholder="预警类型">
          <el-option
            v-for="item in (data.warningType as any)"
            :key="item.itemValue"
            :label="item.itemText"
            :value="item.itemText"
          />
        </el-select>
        <el-button class="ordinary" @click="search()">搜索</el-button>
      </el-col>
      <el-col :span="6" align="right">
        <el-button v-limit="'XTGL:YJGL:READ'" type="danger" @click="handleDel()">标记已读</el-button>
      </el-col>
    </el-row>

    <Table
      :loading="data.isLoading"
      :tableArr="data.allTableData"
      :columns="data.columns"
      :page-size="data.params.size"
      :page-num="data.params.current"
      @selectList="getCurrentList"
    >
      <template #status>
        <el-table-column label="读取状态" width="100">
          <template #default="{ row }" class="status">
            <span class="approved" v-if="row.readFlag == 1">已读</span>
            <span class="rejection" v-if="row.readFlag == 0">未读</span>
          </template>
        </el-table-column>
      </template>
      <template #operate="{ row }">
        <el-button v-limit="'XTGL:YJGL:EDIT'" class="ordinary" @click="handleEdit(row)">反馈</el-button>
      </template>
    </Table>
    <div class="example-pagination-block">
      <el-pagination
        background
        layout="prev, pager, next ,total,sizes,jumper"
        :total="data.total"
        :page-size="data.params.size"
        v-model:current-page="data.params.current"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
    <el-dialog v-model="data.editDialog" width="60%" title="预警详情">
      <Information ref="formRef" v-if="data.editDialog" :content="data.content"></Information>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.editDialog = false">关闭</el-button>
          <el-button type="primary" @click="handleSave()">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { reactive, ref } from 'vue'
import Information from './information.vue'
import tableData from './tableHeader.json'

import { ElMessage, ElMessageBox } from 'element-plus'
import { backWraning, getAllUser, getWraningDict, getWraningList } from '../api/wraning'
import Table from '../components/table2/table.vue'

export default {
  components: {
    Table,
    Information,
  },
  setup() {
    let data = reactive({
      params: {
        level: '',
        initiator: '',
        type: '',
        current: 1,
        size: 10,
      },
      total: 0,
      isLoading: false,
      allTableData: [],
      columns: tableData.platformTableHeader,
      ids: [],
      currentList: [],
      editDialog: false,
      content: {},
      allUserList:[],
      warningLevel:[],
      warningType:[],
    })
    const formRef = ref()
    const getCurrentList = (currentList) => {
      console.log(currentList, 'getCurrentList')
      data.currentList = currentList
      data.ids = currentList.map((item) => {
        return item.id
      })
    }
    const search = () => {
      data.params.current = 1
      onSearch()
    }
    const onSearch = async () => {
      data.isLoading = true
      getWraningList(data.params).then((res) => {
        console.log(res, 'getWraningList')
        data.isLoading = false
        data.allTableData = res.data?.data?.records
        data.total = res.data.data?.total
      })
    }
    onSearch()
    //改变页码
    const handleCurrentChange = (e) => {
      console.log(e, 'handleCurrentChange')
      data.params.current = e
      onSearch()
    }
    //改变页数限制
    const handleSizeChange = (e) => {
      console.log(e, 'handleSizeChange')
      data.params.size = e
      onSearch()
    }
    const handleEdit = (row) => {
      data.editDialog = true
      console.log(row, 'handleEdit')
      data.content = row
    }
    const handleDel = () => {
      if (data.ids.length) {
        ElMessageBox.confirm('您确认标记所选中数据为已读？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
          callback: (action) => {
            if (action == 'confirm') {
              // delBatchProject(data.ids).then((res) => {
              //   console.log(res)
              //   onSearch()
              //   ElMessage({
              //     message: '操作成功',
              //     type: 'success',
              //   })
              // })
            }
          },
        })
      } else {
        ElMessage({
          message: '请至少选择一条数据',
          type: 'warning',
        })
      }
    }
    const handleAdd = () => {
      data.editDialog = true
    }
    const handleSave = () => {
      let params = formRef.value.submitForm()
      if (params) {
        backWraning(params).then((res) => {
          if (res.data.code == 200) {
            data.editDialog = false
            onSearch()
            ElMessage({
              message: '反馈成功',
              type: 'success',
            })
          }
        })
      }
    }
    getWraningDict().then(res=>{
      console.log(res,'getWraningDict');
      data.warningLevel = res.data?.data?.XTGL_WARNING_GRADE
      data.warningType = res.data?.data?.XTGL_WRANING_TYPE
    })
    getAllUser().then((res) => {
      data.allUserList = res.data.data
      console.log(data.allUserList, 'data.allUserList')
    })
    return {
      data,
      formRef,
      onSearch,
      getCurrentList,
      handleCurrentChange,
      handleSizeChange,
      handleEdit,
      handleDel,
      handleAdd,
      handleSave,
      search,
    }
  },
}
</script>

<style lang="scss" scoped>
.container {
  background-color: white;
  padding: 20px;
}
</style>
